package top.dreamyy.pattern.util.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Scanner;

public class AccessCount {
	BigInteger count  = null;
	public BigInteger load(File file){
		//读写文件必须使用try catch
		try{
			//判断文件是否存在
			if(file.exists()){
				Scanner sc = null;
				sc = new Scanner(new FileInputStream(file));
				//判断文件是否有内容
				if(sc.hasNext()){
					System.out.print("文件存在");
					count  = new BigInteger(sc.next());
				}
				sc.close();
			}else{//若不存在文件,新建一个文件，保存数字0，只有第一次可能出现这种情况
				count = new BigInteger("0");
				save(file,count);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}
	public void save(File file,BigInteger count){
		PrintStream printStream = null;
		try{
			//定义输入流
			printStream = new PrintStream(new FileOutputStream(file));
			printStream.println(count);
			printStream.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
